package com.dragon.mylibrary.db;

import android.database.Cursor;
import android.util.Log;


import com.dragon.mylibrary.info.AlarmInfo;
import com.dragon.utilslibrary.dblib.BaseDataDao;

import java.util.List;

/**
 * 闹钟模块的DAO
 */
public class AlarmDao extends BaseDataDao<AlarmInfo> {

    private static final String TAG = "AlarmDao";

    @Override
    public String createTable() {
        return "create table if not exists t_alarm(" +
                "name Text," +
                "time Text," +
                "frequency Text," +
                "isOpen Integer," +
                "interval Integer," +
                "alarmId Integer primary key AUTOINCREMENT);";
    }

    @Override
    public List<AlarmInfo> query(String sql) {
        String querySql = "select * from t_alarm where " + sql + "";
        Log.i(TAG, "query: querySql--->" + querySql);
        Cursor cursor = getCursor(querySql);
        List<AlarmInfo> resultList = getResult(cursor, new AlarmInfo());
        return resultList;
    }

    @Override
    public boolean insertOrReplace(AlarmInfo entity) {
        return super.insertOrReplace(entity);
    }

    public void insertInfoList(List<AlarmInfo> list) {
        super.insertOrReplace(list);
    }

    public List<AlarmInfo> getAll() {
        List<AlarmInfo> list = query(new AlarmInfo());
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    /**
     * 根据闹钟ID查收相应闹钟
     *
     * @param alarmId
     * @return
     */
    public AlarmInfo getAlarmInfoByAlarmId(String alarmId) {
        AlarmInfo info = null;
        String sql = "alarmId " + "='" + alarmId + "'";
        List<AlarmInfo> list = query(sql);
        if (list != null && list.size() > 0) {
            info = list.get(0);
        }
        return info;
    }

    /**
     * 根据闹钟时间查收相应闹钟
     *
     * @param time
     * @return
     */
    public AlarmInfo getAlarmInfoByTime(String time) {
        AlarmInfo info = null;
        String sql = "time " + "='" + time + "'";
        List<AlarmInfo> list = query(sql);
        if (list != null && list.size() > 0) {
            info = list.get(0);
        }
        return info;
    }

    /**
     * 删除单个闹钟
     *
     * @param info
     * @return
     */
    public int deleteAlarm(AlarmInfo info) {
        AlarmInfo alarmInfo = new AlarmInfo();
        alarmInfo.setAlarmId(info.getAlarmId());
        int delete = delete(alarmInfo);
        return delete;
    }

    /**
     * 删除表中所有数据，即所有闹钟
     *
     * @return
     */
    public int deleteAll() {
        int delete = delete(new AlarmInfo());
        return delete;
    }

}
